using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;

namespace NavsGo
{
    public delegate void ExecuteFunc();

    public partial class iPhoneMenu : Form
    {
        Bitmap backgroundImage;
        Graphics gxBuffer;
        Bitmap offBitmap;
        string path;

        private List<ImageButton> ImageButtonList = new List<ImageButton>();

        public bool CloseApp = false;
        bool NeedRepaint = false;

        public iPhoneMenu()
        {
            InitializeComponent();

            timerCheckButtonPressed.Enabled = true;

            int iStart = 15;
            int iStep = 55;
            int iStepY = 65;

            Point[] ButtonGridPosition = { new Point(iStart, iStart),
                new Point(iStart+iStep,iStart+iStepY), 
                new Point(iStart+iStep*2,iStart+iStepY*2),
                new Point(iStart+iStep*3,iStepY*4),
               };


            offBitmap = new Bitmap(this.Width, this.Height);
            gxBuffer = Graphics.FromImage(offBitmap);
            path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            backgroundImage = Properties.Resources.Wallpaper6;

            int i = 0;
            AppInfo appInfo = CreateAppInfo("GoogleMaps", @"\My Storage\Program Files\GoogleMaps\GoogleMaps.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("TomTom", @"\My Storage\Program Files\Navigator\TomTom Navigator.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("Garmin", @"\My Storage\Garmin Mobile XT.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("iGO", @"\Program Files\iGO8\iGO8.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("ROUTE66", @"\My Storage\ROUTE8\ROUTE66N8.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("GPS_XTRA", @"\Windows\GPS_XTRA.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            //appInfo = CreateAppInfo("GPSSettings", @"\Windows\GPSSettings.exe");
            //AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("Port Splitter", @"\My Storage\Program Files\Port Splitter\Port Splitter.exe");
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("Monitor", Properties.Resources.Monitor, StartMonitor);
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("Diagnostics", Properties.Resources.Diagnostics, StartDiagnostics);
            AddImageButton(i++, ButtonGridPosition, appInfo);

            appInfo = CreateAppInfo("Close", Properties.Resources.Close, CloseForm);
            AddImageButton(i++, ButtonGridPosition, appInfo);

            PaintAllButton();
        }

        private void AddImageButton(int index, Point[] ButtonGridPosition, AppInfo appInfo)
        {
            try
            {
                int x = index % 4;
                int y = index / 4;
                ImageButton imageButton = new ImageButton(this, new Point(ButtonGridPosition[x].X, ButtonGridPosition[y].Y));
                imageButton.Image = appInfo.Image;
                imageButton.Tag = appInfo;
                ImageButtonList.Add(imageButton);
            }
            catch
            {
            }
        }

        private AppInfo CreateAppInfo(string name, Bitmap image, ExecuteFunc func)
        {
            AppInfo appInfo = new AppInfo();
            appInfo.Executable = false;
            appInfo.Name = name;
            appInfo.Image = image;
            appInfo.Func = func;
            return appInfo;
        }

        private AppInfo CreateAppInfo(string name, string path)
        {
            if (!File.Exists(path))
            {
                return null;
            }
            AppInfo appInfo = new AppInfo();
            appInfo.Executable = true;
            appInfo.Name = name;
            appInfo.Path = path;
            // Get image
            appInfo.Image = ExtractIcon.GetBitmapFromExeIcon(appInfo.Path);

            return appInfo;
        }

        private void PaintAllButton()
        {
            foreach(ImageButton imageButton in ImageButtonList)
            {
                imageButton.Paint(gxBuffer);
            }
        }

        private void MainMenu_Load(object sender, EventArgs e)
        {            

        }

        
        protected override void OnPaint(PaintEventArgs e)
        {
            gxBuffer.Clear(this.BackColor);
            //Draw background first
            
            gxBuffer.DrawImage(backgroundImage, new Rectangle(0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height), new Rectangle(0, 0, backgroundImage.Width, backgroundImage.Height), GraphicsUnit.Pixel);
            //gxBuffer.DrawImage(backgroundImage, 0, 0);
            PaintAllButton();

            e.Graphics.DrawImage(offBitmap, 0, 0);
        }

        private bool CheckButtonPress()
        {
            foreach (ImageButton imageButton in ImageButtonList)
            {
                if (imageButton.IsPressedOneTime)
                {
                    imageButton.IsPressedOneTime = false;
                    AppInfo appInfo = imageButton.Tag as AppInfo;
                    if (appInfo.Executable)
                    {
                        StartProcess(appInfo.Path);
                    }
                    else
                    {
                        Invoke(appInfo.Func);
                    }
                    return true;
                }
            }
            return false;
        }

        private void StartDiagnostics()
        {
            DiagnosticsForm form = new DiagnosticsForm();
            form.Owner = this;
            form.Show();
        }

        private void StartMonitor()
        {
            MonitorForm form = new MonitorForm();
            form.Owner = this;
            form.Show();
        }

        private void CloseForm()
        {
            Close();
            DialogResult = DialogResult.OK;

            CloseApp = true;
        }

        private void StartProcess(string FileName)
        {
            try
            {
                if (!System.IO.File.Exists(FileName))
                    return;

                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo.UseShellExecute = true;
                myProcess.StartInfo.FileName = FileName;


                myProcess.Start();

                //   myProcess.WaitForExit();                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void PaintForm()
        {

            IntPtr activeWindowHandle = PlatformAPIs.GetForegroundWindow();
            IntPtr thisWindowHandle = this.Handle;
            if (activeWindowHandle != thisWindowHandle)
            {
                if (DialogResult != DialogResult.OK)
                    NeedRepaint = true;
            }
            else
            {
                if (NeedRepaint)
                {
                    this.Invalidate();
                    NeedRepaint = false;
                }
            }
        }

        private void timerCheckButtonPressed_Tick(object sender, EventArgs e)
        {
            
            try
            {
                CheckButtonPress();

                if (NeedRepaint)
                {               
                    this.Invalidate();
                    PaintForm();
                    return;
                }              
               
            }
            catch 
            {
                Close();
            }
        }


        private void MainMenu_Activated(object sender, EventArgs e)
        {
            NeedRepaint = true;
            timerCheckButtonPressed_Tick(null, null);
        }

        private void MainMenu_Deactivate(object sender, EventArgs e)
        {
            NeedRepaint = false;
        }

        private void MainMenu_Paint(object sender, PaintEventArgs e)
        {
            NeedRepaint = true;
            Console.WriteLine("MainMenu_Paint");

        }

        private void MainMenu_GotFocus(object sender, EventArgs e)
        {
            Console.WriteLine("MainMenu_GotFocus");
            NeedRepaint = true;
            timerCheckButtonPressed_Tick(null, null);
        }

        private void MainMenu_LostFocus(object sender, EventArgs e)
        {
            Console.WriteLine("MainMenu_LostFocus");
            NeedRepaint = false;
        }

        private void MainMenu_Resize(object sender, EventArgs e)
        {            
            NeedRepaint = true;
            Console.WriteLine("MainMenu_Resize");
            
        }

        private void MainMenu_Closed(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
        }

        private void MainMenu_EnabledChanged(object sender, EventArgs e)
        {
            Console.WriteLine("MainMenu_EnabledChanged");
            NeedRepaint = true;
            timerCheckButtonPressed_Tick(null, null);
        }
    }
}